package model.memento;

public class Movement {

	/**
	 * direction of movement
	 */
	private MovementDirection direction;
	/**
	 * col <-> x
	 */
	private int col;
	/**
	 * row <-> y
	 */
	private int row;

	/**
	 * @param direction
	 * @param oldCol
	 * @param oldRow
	 */
	public Movement(MovementDirection direction, int oldCol, int oldRow) {
		this.direction = direction;
		setMovementPosition(direction, oldCol, oldRow);
	}

	private void setMovementPosition(MovementDirection direction, int oldCol, int oldRow) {
		switch (direction) {
		case UP:
			this.row = oldRow - 1;
			this.col = oldCol;
			break;
		case RIGHT:
			this.row = oldRow;
			this.col = oldCol + 1;
			break;
		case DOWN:
			this.row = oldRow + 1;
			this.col = oldCol;
			break;
		case LEFT:
			this.row = oldRow;
			this.col = oldCol - 1;
			break;
		}
	}

	public static MovementDirection getOppositeDirection(
			MovementDirection direction) {
		switch (direction) {
		case UP:
			return MovementDirection.DOWN;
		case RIGHT:
			return MovementDirection.LEFT;
		case DOWN:
			return MovementDirection.UP;
		case LEFT:
			return MovementDirection.RIGHT;
		}
		return null;
	}

	/**
	 * @return the row
	 */
	public int getRow() {
		return this.row;
	}

	/**
	 * @return the col
	 */
	public int getCol() {
		return this.col;
	}

	/**
	 * @return the direction
	 */
	public MovementDirection getDirection() {
		return this.direction;
	}

	/**
	 * @param direction
	 *            the direction to set
	 */
	public void setDirection(MovementDirection direction) {
		this.direction = direction;
	}

	/**
	 * @param col
	 * @param row
	 * @return boolean
	 */
	public boolean isMovementPosition(int col, int row) {
		return this.row == row && this.col == col;
	}

	/**
	 * currentPiece --> nextPiece
	 * 
	 * @return Movement
	 */
	public Movement getNextMovement() {
		return new Movement(direction, col, row);
	}

	/**
	 * backPiece <-- currentPiece
	 * 
	 * @return Movement
	 */
	public Movement getBackMovement() {
		Movement back = new Movement(getOppositeDirection(direction), col, row);
		back.setDirection(this.direction);
		return back;
	}
}
